<?php

class admin_model_content_article extends model
{

    public function get_articles($category_id = -1, $key = '', $status=-1, $order_by = 'ordering', $order_by_dir = 'ASC', $offset = 0, $limit = 20)
    {
        
        $query = 'SELECT * FROM `bone_content_article` WHERE 1 ';
        if ($category_id == 0)
            $query .= ' AND `category_id`=0';
        else if ($category_id > 0)
        {
            $model = bone::get_model('content', 'category');
            $ids = $model->get_sub_category_ids($category_id);
            if (count($ids) > 0)
            {
                $ids[] = $category_id;
                $query .= ' AND `category_id` IN(' . implode(',', $ids) . ')';
            }
            else
                $query .= ' AND `category_id`=' . $category_id;
        }
        if ($key !== '') $query .= ' AND `title` LIKE \'%' . $key . '%\'';
        if($status!=-1) $query .= ' AND `block`='.$status;
        
        $query .= ' ORDER BY `' . $order_by . '` ' . $order_by_dir;

        $db = bone::get_db();
        return $db->get_objs($query, $offset, $limit);
    }

    
    public function get_article_sum($category_id = -1, $key = '', $status=-1)
    {
        $query = 'SELECT COUNT(*) FROM `bone_content_article` WHERE 1 ';
        if ($category_id == 0)
            $query .= ' AND `category_id`=0';
        else if ($category_id > 0)
        {
            $model = bone::get_model('content', 'category');
            $ids = $model->get_sub_category_ids($category_id);
            if (count($ids) > 0)
            {
                $ids[] = $category_id;
                $query .= ' AND `category_id` IN(' . implode(',', $ids) . ')';
            }
            else
                $query .= ' AND `category_id`=' . $category_id;
        }
        
        if ($key !== '') $query .= ' AND `title` LIKE \'%' . $key . '%\'';
        if($status!=-1) $query .= ' AND `block`='.$status;
        
        $db = bone::get_db();
        return $db->get_result($query);
    }

    public function set_home($id)
    {
        $db = bone::get_db();
        $db->execute('UPDATE `bone_content_article` SET `home`=0 WHERE `home`=1');
        $db->execute('UPDATE `bone_content_article` SET `home`=1 WHERE `id`=' . $id);
        return true;
    }

    public function unblock($ids)
    {
        $db = bone::get_db();
        if(!$db->execute('UPDATE `bone_content_article` SET `block`=0 WHERE `id` IN(' . $ids . ')'))
        {
            $this->set_error( $db->get_error() );
            return false;
        }
        return true;
    }

    public function block($ids)
    {
        $db = bone::get_db();
        if(!$db->execute('UPDATE `bone_content_article` SET `block`=1 WHERE `id` IN(' . $ids . ')'))
        {
            $this->set_error( $db->get_error() );
            return false;
        }
        return true;
    }

    public function delete($ids)
    {
        $db = bone::get_db();
        if(!$db->execute('DELETE FROM `bone_content_article` WHERE `id` IN(' . $ids . ')'))
        {
            $this->set_error( $db->get_error() );
            return false;
        }
        return true;
    }

}
?>